package com.ds.esd.bpm.custom.perform;

import com.ds.bpm.client.*;
import com.ds.bpm.engine.BPMException;
import com.ds.bpm.engine.WorkflowClientService;
import com.ds.bpm.enums.process.ProcessInstAtt;
import com.ds.common.JDSException;
import com.ds.config.TreeListResultModel;
import com.ds.context.JDSActionContext;
import com.ds.esd.bpm.custom.service.PerformServiceImpl;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.RequestPathEnum;
import com.ds.esd.custom.api.enums.ResponsePathEnum;
import com.ds.esd.custom.module.annotation.DynLoadAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavTabsViewAnnotation;
import com.ds.esd.custom.nav.annotation.TabsAnnotation;
import com.ds.esd.custom.toolbar.MenuBarMenu;
import com.ds.esd.custom.toolbar.bpm.RouteCustomMenu;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.repository.database.proxy.DSMTableProxy;
import com.ds.esd.util.TabPageUtil;
import com.ds.jds.core.esb.EsbUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@MenuBarMenu(menuClasses = PerformServiceImpl.class)
@TabsAnnotation(autoReload = false)
public class DisplayForm {
    @RouteCustomMenu(routeType = {RouteToType.Reload})
    @NavTabsViewAnnotation()
    @DynLoadAnnotation
    @RequestMapping(method = RequestMethod.POST, value = "NewProcess")
    @APIEventAnnotation(customRequestData = {RequestPathEnum.treegridrow, RequestPathEnum.ctx}, customResponseData = ResponsePathEnum.form)
    @DialogAnnotation
    @ModuleAnnotation(dynLoad = true, imageClass = "bpmfont bpmgongzuoliuzhutiguizeweihuguanli", caption = "新建流程", name = "table")
    public @ResponseBody
    TreeListResultModel<List<FormTabs>> newProcess(String processDefVersionId, String activityInstId, String processInstId) {
        TreeListResultModel<List<FormTabs>> formTabsTree = new TreeListResultModel<>();

        List<DSMTableProxy> tableProxyList = new ArrayList<>();
        try {
            ProcessDefVersion version = getClient().getProcessDefVersion(processDefVersionId);
            ProcessInst processInst = getClient().newProcess(version.getProcessDefId(), null, null, null);
            ActivityInst activityInst = processInst.getActivityInstList().get(0);
            JDSActionContext.getActionContext().getContext().put("activityInstId", activityInst.getActivityInstId());
            processInst.getRightAttribute(ProcessInstAtt.PROCESS_INSTANCE_STARTER);
            ProcessDefVersion processDefVersion = activityInst.getProcessDefVersion();
            String domainId = processDefVersion.getAttribute("domainId");
            ProcessDefForm formDef = processDefVersion.getFormDef();
            List<String> tableNames = formDef.getTableNames();
            for (String tableName : tableNames) {
                DSMTableProxy proxy = DSMFactory.getInstance().getRepositoryManager().getTableProxyByName(tableName, domainId);
                tableProxyList.add(proxy);
            }

            formTabsTree = TabPageUtil.getTabList(tableProxyList, FormTabs.class);
            formTabsTree.addCtx("activityInstId", activityInstId);
            formTabsTree.addCtx("processInstId", processInstId);

        } catch (BPMException e) {
            e.printStackTrace();
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return formTabsTree;
    }

    /**
     * @return
     */
    public WorkflowClientService getClient() {
        WorkflowClientService client = ((WorkflowClientService) EsbUtil.parExpression("$BPMC"));
        return client;
    }
}
